package com.hkbigdata.reallywar;

import com.hkbigdata.bean.UserBehavior;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;

/**
 * @author liuanbo
 * @creat 2024-04-19-17:41
 * @see 2194550857@qq.com
 */
public class Flink03_Project_UV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        env.readTextFile("input/UserBehavior.csv")
                .flatMap((String line, Collector<Tuple2<String, Long>> out) -> {
                    String[] split = line.split(",");
                    UserBehavior userBehavior = new UserBehavior(
                            Long.valueOf(split[0]),
                            Long.valueOf(split[1]),
                            Integer.valueOf(split[2]),
                            split[3],
                            Long.valueOf(split[4])
                    );
                    if ("pv".equals(userBehavior.getBehavior())) {
                        out.collect(Tuple2.of("UV", userBehavior.getUserId()));
                    }
                }).returns(Types.TUPLE(Types.STRING, Types.LONG)).keyBy(data -> data.f0).process(new KeyedProcessFunction<String, Tuple2<String, Long>, Long>() {
            HashSet<Long> userids = new HashSet();

            @Override
            public void processElement(Tuple2<String, Long> value, Context ctx, Collector<Long> out) throws Exception {
                userids.add(value.f1);
                int size = userids.size();
                Long aLong = Long.valueOf(size);
                out.collect(aLong);
            }
        }).print();

        env.execute();

    }
}
